
import 'package:extended_text/extended_text.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:osc/common/MyTextStyle.dart';
import 'package:osc/models/entity/osc_about_entity.dart';
import 'package:osc/models/entity/osc_image_entity.dart';
import 'package:osc/models/entity/osc_tweet.dart';
import 'package:osc/res/colors.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/ui/pages/gallery_page.dart';
import 'package:osc/ui/pages/project/project_detail_page.dart';
import 'package:osc/ui/pages/tweet/topic_tweet_page.dart';
import 'package:osc/ui/pages/user/user_profile.dart';
import 'package:osc/ui/widgets/image_loader.dart';
import 'package:osc/ui/widgets/user/osc_author_widget.dart';
import 'package:osc/ui/widgets/small_widgets.dart';
import 'package:osc/ui/widgets/web_scaffold.dart';
import 'package:osc/utils/navigator_util.dart';
import 'package:osc/utils/special_text/my_special_text_span_builder.dart';
import 'package:osc/utils/special_text/special_text_bean.dart';
import 'package:osc/utils/special_text/tweet_origin_author_text.dart';

///动弹内容
class TweetContent extends StatefulWidget {
  final OscTweet tweet;

  const TweetContent({Key key, @required this.tweet}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new TweetContentState(tweet);
  }
}

class TweetContentState extends State<TweetContent> {
  final OscTweet item;
  TweetContentState(this.item);

  double paddingParentHorizontal = Dimens.gap_activity_horizontal;
  double paddingChildHorizontal = Dimens.gap_view_normal;
  double paddingChildVertical = Dimens.gap_view_normal;

  List<String> imageUrls;

  @override
  void initState() {
    super.initState();
    if(item.images != null){
      imageUrls = List();
      for(OscImage image in item.images){
        imageUrls.add(image.thumb);
      }
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          buildUser(),
          buildContent(),
          buildAbout(),
          SmallWidgets.buildLine(),
        ],
      ),
    );
  }

  buildUser(){
    return Padding(
        padding: EdgeInsets.only(left: paddingParentHorizontal, right: paddingParentHorizontal, top: paddingParentHorizontal, bottom: paddingChildVertical),
      child: OscAuthorWidget(
        author: item.author,
        date: item.pubDate,
      ),
    );
  }


  buildContent(){
    Widget widget;
    if(item.images == null || item.images.length == 0){
      widget = buildContentText(item.content);
    } else {
      widget = Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          buildContentText(item.content),
          SmallWidgets.buildLine(color: Colors.transparent, height: Dimens.gap_divider_height_normal),
          buildContentImages(item.images)
        ],
      );
    }

    return Padding(
      padding: EdgeInsets.symmetric(horizontal: paddingParentHorizontal, vertical: paddingChildVertical),
      child: widget,
    );
  }

  buildAbout(){
    OscAbout about = item.about;
    if(about == null) {
      return Offstage(
        offstage: true,
      );
    } else {
      return Container(
        width: ScreenUtil.getScreenW(context),
        color: Colours.bg_tweet_about,
        padding: EdgeInsets.symmetric(horizontal: paddingParentHorizontal,
            vertical: paddingChildVertical),
        child: buildContentText("${TweetOriginAuthorText.beginFlag}${about.title}${TweetOriginAuthorText.stopFlag}：${about.content}"),
      );
    }
  }

  buildTextSpan({String text, Color color = Colors.black54, double fontSize = Dimens.font_sp14, FontWeight fontWeight = FontWeight.normal}){
    return TextSpan(
      text: text,
      style: TextStyle(color: color, fontSize: fontSize, fontWeight: fontWeight),
    );
  }

  buildContentImages(List<OscImage> images){
    return GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
          crossAxisSpacing: Dimens.gap_view_smaller,
          mainAxisSpacing: Dimens.gap_view_smaller,
        ),
        itemBuilder: (context, index){
          return buildImageItem(index, images);
        },
      itemCount: images.length,
      shrinkWrap: true,
      primary: false,
    );
  }

  buildImageItem(int index, List<OscImage> images){
    return GestureDetector(
      onTap: (){
        jump2Page(GalleryPage(imageUrls: imageUrls, index: index,));
      },
      child: ImageLoader(url: images.elementAt(index).thumb, placeholderImageName: "ic_default_image",),
    );
  }

  buildContentText(String text){
    return ExtendedText(
      text,
      style: MyTextStyle.TWEET_CONTENT,
      onSpecialTextTap: (dynamic parameter) {
        if(parameter is SpecialTextBean){
          SpecialTextBean bean = parameter;
          switch(bean.type){
            case SpecialTextBean.TYPE_PROJECT:
              jump2Page(ProjectDetailPage(ident: bean.value,));
              break;
            case SpecialTextBean.TYPE_USER:
              jump2Page(UserProfile(userId: bean.id, nickName: bean.value,));
              break;
            case SpecialTextBean.TYPE_TOPIC:
              jump2Page(TopicTweetPage(topicName: bean.value,));
              break;
            case SpecialTextBean.TYPE_URL:
              jump2Page(WebScaffold(url: bean.value, title: "",));
              break;
          }
        }
      },
      specialTextSpanBuilder: MySpecialTextSpanBuilder(),
      overflow: TextOverflow.ellipsis,
      maxLines: 10,
    );
  }

  jump2Page(Widget widget){
    NavigatorUtil.pushPage(context, widget);
  }
}
